package com.buba.config;

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author xhh
 * @describe 描述
 * @date 2022/6/14 20:24
 * @ClassName DateConvertersConfig
 */
@Configuration
public class DateConvertersConfig {
    private String date_time_pattern = "yyyy-MM-dd HH:mm:ss";
    private String date_pattern = "yyyy-MM-dd";
    private String time_pattern = "HH:mm:ss";
    /**
     * LocalDateTime转换器，用于转换RequestParam和PathVariable参数
     */
    @Bean
    public Converter<String, LocalDateTime> LocalDateTimeConvert() {
        return new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String source) {
                LocalDateTime date = null;
                try {
                    date = LocalDateTime.parse(source, DateTimeFormatter.ofPattern(date_time_pattern));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return date;
            }
        };
    }
    /**
     * LocalDate转换器，用于转换RequestParam和PathVariable参数
     */
    @Bean
    public Converter<String, LocalDate> LocalDateConvert() {
        return new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String source) {
                LocalDate date = null;
                try {
                    date = LocalDate.parse(source,DateTimeFormatter.ofPattern(date_pattern));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return date;
            }
        };
    }
    /**
     * LocalTime转换器，用于转换RequestParam和PathVariable参数
     */
    @Bean
    public Converter<String, LocalTime> LocalTimeConvert() {
        return new Converter<String, LocalTime>() {
            @Override
            public LocalTime convert(String source) {
                LocalTime date = null;
                try {
                    date = LocalTime.parse(source,DateTimeFormatter.ofPattern(time_pattern));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return date;
            }
        };
    }

    /**
     * Response返回时格式化
     * @return
     */
    @Bean
    public LocalDateTimeSerializer localDateTimeDeserializer() {
        return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(date_time_pattern));
    }
    @Bean
    public LocalDateSerializer localDateDeserializer() {
        return new LocalDateSerializer(DateTimeFormatter.ofPattern(date_pattern));
    }
    @Bean
    public LocalTimeSerializer localTimeDeserializer() {
        return new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm"));
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> builder
                .serializerByType(LocalDateTime.class, localDateTimeDeserializer())
                .serializerByType(LocalDate.class,localDateDeserializer())
                .serializerByType(LocalTime.class,localTimeDeserializer())
                ;
    }
}

